package com.yy.job.core.config.validator;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;
import com.yy.job.common.enums.ErrorCodeEnum;
import com.yy.job.core.config.xml.ModuleXmlConfig;
import com.yy.job.core.config.xml.ProjectXmlConfig;
import com.yy.job.utils.exception.YYJobRuntimeException;

/**
 * 校验器
 * 
 * @author eleven.li
 *
 */
public class ProjectXmlConfigValidator {
	
	public static void validate(ProjectXmlConfig projectXmlConfig) {
		if(projectXmlConfig == null) {
			throw new YYJobRuntimeException(ErrorCodeEnum.CHECK_PROJECT_CONFIG, "no yy-job config");
		}
		
		if(StringUtils.isEmpty(projectXmlConfig.getProjectCode())) {
			throw new YYJobRuntimeException(ErrorCodeEnum.CHECK_PROJECT_ERROR, "projectCode is empty");		
		}
		
		List<ModuleXmlConfig> moduleConfigs = projectXmlConfig.getModuleConfigs();
		if (CollectionUtils.isEmpty(moduleConfigs)) {
			return;
		}

		List<String> duplicateJobCodeList = Lists.newArrayList();
		for (ModuleXmlConfig moduleConfig : moduleConfigs) {
			// 校验moduleJobConfigs节点的配置
			ModuleXmlConfigValidator.validate(moduleConfig);

			// 校验moduleCode是否重复
			String moduleCode = moduleConfig.getModuleCode();
			if (duplicateJobCodeList.contains(moduleCode)) {
				throw new YYJobRuntimeException(ErrorCodeEnum.CHECK_MODULE_CONFIG, moduleCode, "moduleCode is duplicated");
			}
			duplicateJobCodeList.add(moduleCode);
		}
	}
	
}
